<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of doggy_admin
 *
 * @author Gidaff_01
 */
class doggy_admin extends Admin_Controller {

    //put your code here
    function Doggy_admin() {
        parent::Admin_Controller();
        $this->load->model('Doggy_admin_m');
        $this->load->helper('date');
        $this->_data['erreur'] = "";
    }

    function index() {
        is_admin();
        $this->list_member();
    }

    function logout() {
        $this->session->unset_userdata('adminid');
        redirect(base_url() . 'index.php/admin');
    }

    function list_member() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/doggy_admin/list_member/'. $per_page . '/';
        $config['per_page'] = '5';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Doggy_admin_m->count_member();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Doggy_admin_m->get_all_member($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Doggy_admin_m->count_member();
        $this->_data['page_title'] = "Liste de member";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_member');
    }

    function list_race() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/doggy_admin/list_race/' . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Doggy_admin_m->count_race();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Doggy_admin_m->list_race($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Doggy_admin_m->count_race();
        $this->_data['page_title'] = "Liste de race";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_race');
    }

    function delete_member($id, $per_page, $off_set) {
        if ($this->session->userdata('level') == 1) {
            $this->Doggy_admin_m->delete_sub_account($id);
            $this->Doggy_admin_m->delete_member($id);
        } else {
            $this->_data['erreur'] = "Vous n'avez pas la permission";
        }
        redirect(site_url('doggy_admin/list_member') . '/' . $per_page . '/' . $off_set);
    }

    function add_race() {
        $this->_data['page_title'] = "Add race";
        is_admin();
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'race_name',
                    'Label' => 'Race name',
                    'rules' => 'required|callback_check_exist_racename'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $race_name = $this->input->post('race_name');
                $data = array('race_name' => $race_name
                );
                $this->Doggy_admin_m->add_race($data);
                redirect('doggy_admin/list_race');
            } else {
                $this->display_view('admin/add_race');
            }
        } else {
            $this->display_view('admin/add_race');
        }
    }

    function check_exist_racename() {
        $racename = $this->input->post('race_name');
        $query = $this->Doggy_admin_m->get_exist_race($racename);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_racename', 'The race name was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function delete_race($id,$per_page,$off_set) {
        is_admin();
        if ($this->check_exist_sub_race($id)) {
            $this->session->set_userdata('error', '');
            $this->Doggy_admin_m->delete_race($id);
            redirect('doggy_admin/list_race'.'/'.$per_page.'/'.$off_set);
        } else {
            $this->session->set_userdata('error', 'Have a member in race! Cannot delete!');
            $this->display_view('admin/delete_fail');
        }
    }

    function edit_race_name($id) {
        $this->_data['page_title'] = "Edit race";
        is_admin();
        $race = $this->Doggy_admin_m->get_race_by_id($id);
        $this->_data['race'] = $race;
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'race_name',
                    'Label' => 'Race name',
                    'rules' => 'required|callback_check_exist_racename'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $data = array('race_name' => $this->input->post('race_name'));
                $query = $this->Doggy_admin_m->edit_race_name($data, $id);
                redirect('doggy_admin/list_race');
            } else {
                $this->display_view('admin/edit_race_name');
            }
        } else {
            $this->display_view('admin/edit_race_name');
        }

        if ($this->input->post('back')) {
            redirect('doggy_admin/list_race');
        }
    }

    //horoscope
    function add_horoscope() {
        is_admin();
        $this->_data['page_title'] = "Add horoscope";
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'name',
                    'Label' => 'Horoscope name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'description',
                    'Label' => 'Description',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $image_path = './../assets/avatar';
                $thumb_path = $image_path . '/images';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    delete_pro_file($upload_info['full_path']);

                    $name = $this->input->post('name');
                    $data = array('name' => $name,
                        'start_date' => $this->input->post('start_date'),
                        'end_date' => $this->input->post('end_date'),
                        'start_month' => $this->input->post('start_month'),
                        'end_month' => $this->input->post('end_month'),
                        'description' => $this->input->post('description'),
                        'avatar' => $upload_info['file_name']
                    );
                    $this->Doggy_admin_m->add_horoscope($data);
                    redirect('doggy_admin/list_horoscope');
                } else {
                    $name = $this->input->post('name');
                    $data = array('name' => $name,
                        'start_date' => $this->input->post('start_date'),
                        'end_date' => $this->input->post('end_date'),
                        'start_month' => $this->input->post('start_month'),
                        'end_month' => $this->input->post('end_month'),
                        'description' => $this->input->post('description')
                    );
                    $this->Doggy_admin_m->add_horoscope($data);
                    redirect('doggy_admin/list_horoscope');
                }
            } else {
                $this->display_view('admin/add_horoscope');
            }
        } else {
            $this->display_view('admin/add_horoscope');
        }
    }

    function check_exist_horoscope_name() {
        $name = $this->input->post('name');
        $query = $this->Doggy_admin_m->get_exist_horoscope_name($name);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_horoscope_name', 'The horoscope name was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function check_exist_sub_race($id) {
        $query = $this->Doggy_admin_m->get_exist_sub_race($id);
        if ($query->num_rows() > 0) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function delete_horoscope($id,$per_page,$off_set) {
        is_admin();
        $this->Doggy_admin_m->delete_horoscope($id);
        redirect('doggy_admin/list_horoscope'.'/'.$per_page.'/'.$off_set);
    }

    function list_horoscope() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/doggy_admin/list_horoscope/' . '/' . $per_page . '/';
        $config['per_page'] = '5';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Doggy_admin_m->count_horoscope();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Doggy_admin_m->list_horoscope($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Doggy_admin_m->count_horoscope();
        $this->_data['page_title'] = "Liste de horoscope";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_horoscope');
    }

    function edit_horoscope($id) {
        is_admin();
        $this->_data['page_title'] = "Edit horoscope";
        $horoscope = $this->Doggy_admin_m->get_horoscope_by_id($id);
        $this->_data['horoscope'] = $horoscope;

        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'description',
                    'Label' => 'Description',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $image_path = './../assets/avatar';
                $thumb_path = $image_path . '/images';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    delete_pro_file($upload_info['full_path']);

                    $name = $this->input->post('name');
                    $data = array(
                        'description' => $this->input->post('description'),
                        'avatar' => $upload_info['file_name']
                    );
                    $this->Doggy_admin_m->edit_horoscope($data,$id);
                    redirect('doggy_admin/list_horoscope');
                } else {
                    $name = $this->input->post('name');
                    $data = array(
                        'description' => $this->input->post('description')
                    );
                    $this->Doggy_admin_m->edit_horoscope($data, $id);
                    redirect('doggy_admin/list_horoscope');
                }
            } else {
                $this->display_view('admin/edit_horoscope');
            }
        } else {
            $this->display_view('admin/edit_horoscope');
        }
    }

    //parkway
    function add_parkway() {
        is_admin();
        $this->_data['page_title'] = "Add parkway";
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'name',
                    'Label' => 'Parkway name',
                    'rules' => 'required|callback_check_exist_parkway_name'
                ),
                array(
                    'field' => 'description',
                    'Label' => 'Description',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $image_path = './assets/parkway';
                $thumb_path = $image_path . '/images';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config

                    $name = $this->input->post('name');
                    $data = array('name' => $name,
                        'region_id' => $this->input->post('region_id'),
                        'description' => $this->input->post('description'),
                        'image' => $upload_info['file_name']
                    );
                    $this->Doggy_admin_m->add_parkway($data);
                    redirect('doggy_admin/list_parkway');
                } else {
                    $name = $this->input->post('name');
                    $data = array('name' => $name,
                        'region_id' => $this->input->post('region_id'),
                        'description' => $this->input->post('description')
                    );
                    $this->Doggy_admin_m->add_parkway($data);
                    redirect('doggy_admin/list_parkway');
                }
            } else {
                $this->display_view('admin/add_parkway');
            }
        } else {
            $this->display_view('admin/add_parkway');
        }
    }

    function check_exist_parkway_name() {
        $name = $this->input->post('name');
        $query = $this->Doggy_admin_m->get_exist_parkway_name($name);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_exist_parkway_name', 'The parkway name was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function delete_parkway($id,$per_page,$off_set) {
        is_admin();
        $this->Doggy_admin_m->delete_parkway($id);
        redirect('doggy_admin/list_parkway'.'/'.$per_page.'/'.$off_set);
    }

    function list_parkway() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/doggy_admin/list_parkway/' . '/' . $per_page . '/';
        $config['per_page'] = '5';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Doggy_admin_m->count_parkway();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Doggy_admin_m->list_parkway($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Doggy_admin_m->count_parkway();
        $this->_data['page_title'] = "Liste de parkway";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('admin/list_parkway');
    }

    function edit_parkway($id) {
        is_admin();
        $this->_data['page_title'] = "Edit parkway";
        $parkway = $this->Doggy_admin_m->get_parkway_by_id($id);
        $this->_data['parkway'] = $parkway;

        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'description',
                    'Label' => 'Description',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run()) {
                $image_path = './assets/parkway';
                $thumb_path = $image_path . '/images';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config

                    $data = array('name'=> $this->input->post('name'),
                        'region_id' => $this->input->post('region_id'),
                        'description' => $this->input->post('description'),
                        'image' => $upload_info['file_name']
                    );
                    $this->Doggy_admin_m->edit_parkway($data,$id);
                    redirect('doggy_admin/list_parkway');
                } else {
                    $name = $this->input->post('name');
                    $data = array(
                        'name'=> $this->input->post('name'),
                        'region_id' => $this->input->post('region_id'),
                        'description' => $this->input->post('description')
                    );
                    $this->Doggy_admin_m->edit_parkway($data, $id);
                    redirect('doggy_admin/list_parkway');
                }
            } else {
                $this->display_view('admin/edit_parkway');
            }
        } else {
            $this->display_view('admin/edit_parkway');
        }
    }

    function search_parkway() {
        if ($this->input->post('search')) {
            $region = $this->input->post('search_region');
            $this->_data['selected_region'] = $region;
            if ($region != null && $region != '') {
                $per_page = $this->uri->segment(3);
                if ($per_page == "") {
                    $per_page = 10;
                }
                $off_set = $this->uri->segment(4);
                if ($off_set == "") {
                    $off_set = 0;
                }
                $config['base_url'] = base_url() . 'index.php/doggy_admin/search_parkway_page/' . $region . '/' . $per_page . '/';
                $config['per_page'] = $per_page;
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->Doggy_admin_m->count_search_parkway($region);
                if ($off_set > 0 && $off_set == $config['total_rows']) {
                    $off_set = $off_set - $per_page;
                }
                $this->_data['query'] = $this->Doggy_admin_m->search_parkway($region, $config['per_page'], $off_set);
                $this->pagination->initialize($config);
                $this->_data['pagination'] = $this->pagination->create_links();
                $this->_data['total'] = $this->Doggy_admin_m->count_search_parkway($region);
                $this->_data['page_title'] = "Liste de parkway";
                $this->_data['per_page'] = $per_page;
                $this->_data['off_set'] = $off_set;
                $this->_data['parkway_search'] = $region;
                $this->display_view('admin/list_parkway');
            } else {
                $this->list_parkway();
            }
        } else {
            $this->display_view('admin/list_parkway');
        }
    }

    function search_parkway_page() {
        $region = $this->uri->segment(3);
        $this->_data['selected_region'] = $region;
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/doggy_admin/search_parkway_page/' . $region . '/' . $per_page . '/';
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Doggy_admin_m->count_search_parkway($region);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Doggy_admin_m->search_parkway($region, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Doggy_admin_m->count_search_parkway($region);
        $this->_data['page_title'] = "Liste de parkway";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['parkway_search'] = $region;
        $this->display_view('admin/list_parkway');
    }
}

?>
